﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using IronOne.SalesOne.BusinessObjects;


namespace IronOne.SalesOne.DataObjects
{
    public class LinqIncome
    {

        public List<Income> GetIncomes(string sortExpression)
        {
            
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<IncomeEntity> query = db.IncomeEntities;

                if (sortExpression.Length > 0)
                {
                    string[] sort = sortExpression.Split(' ');
                    string sortColumn = sort[0];
                    string sortOrder = sort[1];

                    switch (sortColumn)
                    {
                        case "IncomeID":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.IncomeID);
                            else
                                query = query.OrderByDescending(c => c.IncomeID);
                            break;

                    }
                }
                return query.Select(c => Mapper.ToIncomeObject(c)).ToList();
            }
        }


        public Income GetIncome(int incomeId)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                return Mapper.ToIncomeObject(db.IncomeEntities
                            .SingleOrDefault(p => p.IncomeID == incomeId));
            }
        }


        public Income GetIncomeForCustomer(string customerId)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                Income inco = new Income();

                var income = (from inc in db.IncomeEntities
                                 where inc.CustomerID == customerId
                                 group inc by new { inc.CustomerID } into g
                                 select new Income
                                     {
                                         IncomeId = g.FirstOrDefault().IncomeID,
                                         CustomerId = g.FirstOrDefault().CustomerID,
                                         IncomeValue = (float)g.Sum(p => (double?)p.Income1).GetValueOrDefault(),
                                         SpousesIncome = (float)g.Sum(p => p.SpouseIncome).GetValueOrDefault(),
                                         OtherIncome = (float)g.Sum(p => (double?)p.OtherIncome).GetValueOrDefault(),
                                         Savings = (float)g.Sum(p => (double?)p.Savings).GetValueOrDefault()

                                     });

                if (income.FirstOrDefault() !=null)
                {
                    inco.IncomeId = income.FirstOrDefault().IncomeId;
                    inco.CustomerId = income.FirstOrDefault().CustomerId;
                    inco.IncomeValue = income.FirstOrDefault().IncomeValue;
                    inco.SpousesIncome = income.FirstOrDefault().SpousesIncome;
                    inco.OtherIncome = income.FirstOrDefault().OtherIncome;
                    inco.Savings = income.FirstOrDefault().Savings;
                }
                else
                {
                    inco.IncomeValue = 0;
                    inco.SpousesIncome = 0;
                    inco.OtherIncome = 0;
                    inco.Savings = 0;
                }

                return inco;

            }
        
        }

        public void InsertIncome(Income income)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    IncomeEntity entity = Mapper.ToIncomeEntity(income);
                    db.IncomeEntities.InsertOnSubmit(entity);
                    db.SubmitChanges();

                    income.IncomeId = entity.IncomeID;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to income record was made before your changes.");
                }
                catch (Exception)
                {
                    throw new Exception("Unable to Insert income record ");
                    //throw new Exception("Unable to Insert income record " + income.Income);
                }
            }
        }

        public int UpdateIncome(Income income)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    IncomeEntity entity = Mapper.ToIncomeEntity(income);
                    db.IncomeEntities.Attach(entity, true);
                    db.SubmitChanges();
                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to income record was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }

        public int DeleteIncome(Income income)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    IncomeEntity entity = Mapper.ToIncomeEntity(income);
                    db.IncomeEntities.Attach(entity, false);
                    db.IncomeEntities.DeleteOnSubmit(entity);
                    db.SubmitChanges();

                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to income record was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }
    }
}
